home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / common.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  36KB  |  1,892 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // common.c -- misc functions used in client and server
  21.  
  22. #include "quakedef.h"
  23.  
  24. #define NUM_SAFE_ARGVS  7
  25.  
  26. static char     *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
  27. static char     *argvdummy = " ";
  28.  
  29. static char     *safeargvs[NUM_SAFE_ARGVS] =
  30.     {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"};
  31.  
  32. cvar_t  registered = {"registered","0"};
  33. cvar_t  cmdline = {"cmdline","0", false, true};
  34.  
  35. qboolean        com_modified;   // set true if using non-id files
  36.  
  37. qboolean        proghack;
  38.  
  39. int             static_registered = 1;  // only for startup check, then set
  40.  
  41. qboolean        msg_suppress_1 = 0;
  42.  
  43. void COM_InitFilesystem (void);
  44.  
  45. // if a packfile directory differs from this, it is assumed to be hacked
  46. #define PAK0_COUNT              339
  47. #define PAK0_CRC                32981
  48.  
  49. char    com_token[1024];
  50. int        com_argc;
  51. char    **com_argv;
  52.  
  53. #define CMDLINE_LENGTH    256
  54. char    com_cmdline[CMDLINE_LENGTH];
  55.  
  56. qboolean        standard_quake = true, rogue, hipnotic;
  57.  
  58. // this graphic needs to be in the pak file to use registered features
  59. unsigned short pop[] =
  60. {
  61.  0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
  62. ,0x0000,0x0000,0x6600,0x0000,0x0000,0x0000,0x6600,0x0000
  63. ,0x0000,0x0066,0x0000,0x0000,0x0000,0x0000,0x0067,0x0000
  64. ,0x0000,0x6665,0x0000,0x0000,0x0000,0x0000,0x0065,0x6600
  65. ,0x0063,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6563
  66. ,0x0064,0x6561,0x0000,0x0000,0x0000,0x0000,0x0061,0x6564
  67. ,0x0064,0x6564,0x0000,0x6469,0x6969,0x6400,0x0064,0x6564
  68. ,0x0063,0x6568,0x6200,0x0064,0x6864,0x0000,0x6268,0x6563
  69. ,0x0000,0x6567,0x6963,0x0064,0x6764,0x0063,0x6967,0x6500
  70. ,0x0000,0x6266,0x6769,0x6a68,0x6768,0x6a69,0x6766,0x6200
  71. ,0x0000,0x0062,0x6566,0x6666,0x6666,0x6666,0x6562,0x0000
  72. ,0x0000,0x0000,0x0062,0x6364,0x6664,0x6362,0x0000,0x0000
  73. ,0x0000,0x0000,0x0000,0x0062,0x6662,0x0000,0x0000,0x0000
  74. ,0x0000,0x0000,0x0000,0x0061,0x6661,0x0000,0x0000,0x0000
  75. ,0x0000,0x0000,0x0000,0x0000,0x6500,0x0000,0x0000,0x0000
  76. ,0x0000,0x0000,0x0000,0x0000,0x6400,0x0000,0x0000,0x0000
  77. };
  78.  
  79. /*
  80.  
  81.  
  82. All of Quake's data access is through a hierchal file system, but the
  83. contents of the file system can be transparently merged from several
  84. sources.
  85.  
  86. The "base directory" is the path to the directory holding the
  87. quake.exe and all game directories.  The sys_* files pass this to
  88. host_init in quakeparms_t->basedir.  This can be overridden with the
  89. "-basedir" command line parm to allow code debugging in a different
  90. directory.  The base directory is only used during filesystem
  91. initialization.
  92.  
  93. The "game directory" is the first tree on the search path and
  94. directory that all generated files (savegames, screenshots, demos,
  95. config files) will be saved to.  This can be overridden with the
  96. "-game" command line parameter.  The game directory can never be
  97. changed while quake is executing.  This is a precacution against
  98. having a malicious server instruct clients to write files over areas
  99. they shouldn't.
  100.  
  101. The "cache directory" is only used during development to save network
  102. bandwidth, especially over ISDN / T1 lines.  If there is a cache
  103. directory specified, when a file is found by the normal search path,
  104. it will be mirrored into the cache directory, then opened there.
  105.  
  106.  
  107.  
  108. FIXME:
  109. The file "parms.txt" will be read out of the game directory and
  110. appended to the current command line arguments to allow different
  111. games to initialize startup parms differently.  This could be used to
  112. add a "-sspeed 22050" for the high quality sound edition.  Because
  113. they are added at the end, they will not override an explicit setting
  114. on the original command line.       */
  115.  
  116. //============================================================================
  117.  
  118.  
  119. // ClearLink is used for new headnodes
  120. void ClearLink (link_t *l)
  121. {
  122.     l->prev = l->next = l;
  123. }
  124.  
  125. void RemoveLink (link_t *l)
  126. {
  127.     l->next->prev = l->prev;
  128.     l->prev->next = l->next;
  129. }
  130.  
  131. void InsertLinkBefore (link_t *l, link_t *before)
  132. {
  133.     l->next = before;
  134.     l->prev = before->prev;
  135.     l->prev->next = l;
  136.     l->next->prev = l;
  137. }
  138. void InsertLinkAfter (link_t *l, link_t *after)
  139. {
  140.     l->next = after->next;
  141.     l->prev = after;
  142.     l->prev->next = l;
  143.     l->next->prev = l;
  144. }
  145.  
  146. /*
  147. ============================================================================
  148.  
  149.                     LIBRARY REPLACEMENT FUNCTIONS
  150.  
  151. ============================================================================
  152. */
  153.  
  154. void Q_memset (void *dest, int fill, int count)
  155. {
  156.     int             i;
  157.  
  158.     if ( (((long)dest | count) & 3) == 0)
  159.     {
  160.         count >>= 2;
  161.         fill = fill | (fill<<8) | (fill<<16) | (fill<<24);
  162.         for (i=0 ; i<count ; i++)
  163.             ((int *)dest)[i] = fill;
  164.     }
  165.     else
  166.         for (i=0 ; i<count ; i++)
  167.             ((byte *)dest)[i] = fill;
  168. }
  169.  
  170. void Q_memcpy (void *dest, void *src, int count)
  171. {
  172.     int             i;
  173.  
  174.     if (( ( (long)dest | (long)src | count) & 3) == 0 )
  175.     {
  176.         count>>=2;
  177.         for (i=0 ; i<count ; i++)
  178.             ((int *)dest)[i] = ((int *)src)[i];
  179.     }
  180.     else
  181.         for (i=0 ; i<count ; i++)
  182.             ((byte *)dest)[i] = ((byte *)src)[i];
  183. }
  184.  
  185. int Q_memcmp (void *m1, void *m2, int count)
  186. {
  187.     while(count)
  188.     {
  189.         count--;
  190.         if (((byte *)m1)[count] != ((byte *)m2)[count])
  191.             return -1;
  192.     }
  193.     return 0;
  194. }
  195.  
  196. void Q_strcpy (char *dest, char *src)
  197. {
  198.     while (*src)
  199.     {
  200.         *dest++ = *src++;
  201.     }
  202.     *dest++ = 0;
  203. }
  204.  
  205. void Q_strncpy (char *dest, char *src, int count)
  206. {
  207.     while (*src && count--)
  208.     {
  209.         *dest++ = *src++;
  210.     }
  211.     if (count)
  212.         *dest++ = 0;
  213. }
  214.  
  215. int Q_strlen (char *str)
  216. {
  217.     int             count;
  218.  
  219.     count = 0;
  220.     while (str[count])
  221.         count++;
  222.  
  223.     return count;
  224. }
  225.  
  226. char *Q_strrchr(char *s, char c)
  227. {
  228.     int len = Q_strlen(s);
  229.  
  230.     s += len;
  231.     while (len--)
  232.     if (*--s == c) return s;
  233.     return 0;
  234. }
  235.  
  236. void Q_strcat (char *dest, char *src)
  237. {
  238.     dest += Q_strlen(dest);
  239.     Q_strcpy (dest, src);
  240. }
  241.  
  242. int Q_strcmp (char *s1, char *s2)
  243. {
  244.     while (1)
  245.     {
  246.         if (*s1 != *s2)
  247.             return -1;              // strings not equal    
  248.         if (!*s1)
  249.             return 0;               // strings are equal
  250.         s1++;
  251.         s2++;
  252.     }
  253.     
  254.     return -1;
  255. }
  256.  
  257. int Q_strncmp (char *s1, char *s2, int count)
  258. {
  259.     while (1)
  260.     {
  261.         if (!count--)
  262.             return 0;
  263.         if (*s1 != *s2)
  264.             return -1;              // strings not equal    
  265.         if (!*s1)
  266.             return 0;               // strings are equal
  267.         s1++;
  268.         s2++;
  269.     }
  270.     
  271.     return -1;
  272. }
  273.  
  274. int Q_strncasecmp (char *s1, char *s2, int n)
  275. {
  276.     int             c1, c2;
  277.  
  278.     while (1)
  279.     {
  280.         c1 = *s1++;
  281.         c2 = *s2++;
  282.  
  283.         if (!n--)
  284.             return 0;               // strings are equal until end point
  285.         
  286.         if (c1 != c2)
  287.         {
  288.             if (c1 >= 'a' && c1 <= 'z')
  289.                 c1 -= ('a' - 'A');
  290.             if (c2 >= 'a' && c2 <= 'z')
  291.                 c2 -= ('a' - 'A');
  292.             if (c1 != c2)
  293.                 return -1;              // strings not equal
  294.         }
  295.         if (!c1)
  296.             return 0;               // strings are equal
  297. //              s1++;
  298. //              s2++;
  299.     }
  300.     
  301.     return -1;
  302. }
  303.  
  304. int Q_strcasecmp (char *s1, char *s2)
  305. {
  306.     return Q_strncasecmp (s1, s2, 99999);
  307. }
  308.  
  309. int Q_atoi (char *str)
  310. {
  311.     int             val;
  312.     int             sign;
  313.     int             c;
  314.  
  315.     if (*str == '-')
  316.     {
  317.         sign = -1;
  318.         str++;
  319.     }
  320.     else
  321.         sign = 1;
  322.         
  323.     val = 0;
  324.  
  325. //
  326. // check for hex
  327. //
  328.     if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  329.     {
  330.         str += 2;
  331.         while (1)
  332.         {
  333.             c = *str++;
  334.             if (c >= '0' && c <= '9')
  335.                 val = (val<<4) + c - '0';
  336.             else if (c >= 'a' && c <= 'f')
  337.                 val = (val<<4) + c - 'a' + 10;
  338.             else if (c >= 'A' && c <= 'F')
  339.                 val = (val<<4) + c - 'A' + 10;
  340.             else
  341.                 return val*sign;
  342.         }
  343.     }
  344.     
  345. //
  346. // check for character
  347. //
  348.     if (str[0] == '\'')
  349.     {
  350.         return sign * str[1];
  351.     }
  352.     
  353. //
  354. // assume decimal
  355. //
  356.     while (1)
  357.     {
  358.         c = *str++;
  359.         if (c <'0' || c > '9')
  360.             return val*sign;
  361.         val = val*10 + c - '0';
  362.     }
  363.     
  364.     return 0;
  365. }
  366.  
  367.  
  368. float Q_atof (char *str)
  369. {
  370.     double            val;
  371.     int             sign;
  372.     int             c;
  373.     int             decimal, total;
  374.  
  375.     if (*str == '-')
  376.     {
  377.         sign = -1;
  378.         str++;
  379.     }
  380.     else
  381.         sign = 1;
  382.         
  383.     val = 0;
  384.  
  385. //
  386. // check for hex
  387. //
  388.     if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
  389.     {
  390.         str += 2;
  391.         while (1)
  392.         {
  393.             c = *str++;
  394.             if (c >= '0' && c <= '9')
  395.                 val = (val*16) + c - '0';
  396.             else if (c >= 'a' && c <= 'f')
  397.                 val = (val*16) + c - 'a' + 10;
  398.             else if (c >= 'A' && c <= 'F')
  399.                 val = (val*16) + c - 'A' + 10;
  400.             else
  401.                 return val*sign;
  402.         }
  403.     }
  404.     
  405. //
  406. // check for character
  407. //
  408.     if (str[0] == '\'')
  409.     {
  410.         return sign * str[1];
  411.     }
  412.     
  413. //
  414. // assume decimal
  415. //
  416.     decimal = -1;
  417.     total = 0;
  418.     while (1)
  419.     {
  420.         c = *str++;
  421.         if (c == '.')
  422.         {
  423.             decimal = total;
  424.             continue;
  425.         }
  426.         if (c <'0' || c > '9')
  427.             break;
  428.         val = val*10 + c - '0';
  429.         total++;
  430.     }
  431.  
  432.     if (decimal == -1)
  433.         return val*sign;
  434.     while (total > decimal)
  435.     {
  436.         val /= 10;
  437.         total--;
  438.     }
  439.     
  440.     return val*sign;
  441. }
  442.  
  443. /*
  444. ============================================================================
  445.  
  446.                     BYTE ORDER FUNCTIONS
  447.  
  448. ============================================================================
  449. */
  450.  
  451. qboolean        bigendien;
  452.  
  453. short   (*BigShort) (short l);
  454. short   (*LittleShort) (short l);
  455. int     (*BigLong) (int l);
  456. int     (*LittleLong) (int l);
  457. float   (*BigFloat) (float l);
  458. float   (*LittleFloat) (float l);
  459.  
  460. short   ShortSwap (short l)
  461. {
  462.     byte    b1,b2;
  463.  
  464.     b1 = l&255;
  465.     b2 = (l>>8)&255;
  466.  
  467.     return (b1<<8) + b2;
  468. }
  469.  
  470. short   ShortNoSwap (short l)
  471. {
  472.     return l;
  473. }
  474.  
  475. int    LongSwap (int l)
  476. {
  477.     byte    b1,b2,b3,b4;
  478.  
  479.     b1 = l&255;
  480.     b2 = (l>>8)&255;
  481.     b3 = (l>>16)&255;
  482.     b4 = (l>>24)&255;
  483.  
  484.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  485. }
  486.  
  487. int     LongNoSwap (int l)
  488. {
  489.     return l;
  490. }
  491.  
  492. float FloatSwap (float f)
  493. {
  494.     union
  495.     {
  496.         float   f;
  497.         byte    b[4];
  498.     } dat1, dat2;
  499.     
  500.     dat1.f = f;
  501.     dat2.b[0] = dat1.b[3];
  502.     dat2.b[1] = dat1.b[2];
  503.     dat2.b[2] = dat1.b[1];
  504.     dat2.b[3] = dat1.b[0];
  505.     return dat2.f;
  506. }
  507.  
  508. float FloatNoSwap (float f)
  509. {
  510.     return f;
  511. }
  512.  
  513. /*
  514. ==============================================================================
  515.  
  516.             MESSAGE IO FUNCTIONS
  517.  
  518. Handles byte ordering and avoids alignment errors
  519. ==============================================================================
  520. */
  521.  
  522. //
  523. // writing functions
  524. //
  525.  
  526. void MSG_WriteChar (sizebuf_t *sb, int c)
  527. {
  528.     byte    *buf;
  529.  
  530. #ifdef PARANOID
  531.     if (c < -128 || c > 127)
  532.         Sys_Error ("MSG_WriteChar: range error");
  533. #endif
  534.  
  535.     buf = SZ_GetSpace (sb, 1);
  536.     buf[0] = c;
  537. }
  538.  
  539. void MSG_WriteByte (sizebuf_t *sb, int c)
  540. {
  541.     byte    *buf;
  542.     
  543. #ifdef PARANOID
  544.     if (c < 0 || c > 255)
  545.         Sys_Error ("MSG_WriteByte: range error");
  546. #endif
  547.  
  548.     buf = SZ_GetSpace (sb, 1);
  549.     buf[0] = c;
  550. }
  551.  
  552. void MSG_WriteShort (sizebuf_t *sb, int c)
  553. {
  554.     byte    *buf;
  555.  
  556. #ifdef PARANOID
  557.     if (c < ((short)0x8000) || c > (short)0x7fff)
  558.         Sys_Error ("MSG_WriteShort: range error");
  559. #endif
  560.  
  561.     buf = SZ_GetSpace (sb, 2);
  562.     buf[0] = c&0xff;
  563.     buf[1] = c>>8;
  564. }
  565.  
  566. void MSG_WriteLong (sizebuf_t *sb, int c)
  567. {
  568.     byte    *buf;
  569.  
  570.     buf = SZ_GetSpace (sb, 4);
  571.     buf[0] = c&0xff;
  572.     buf[1] = (c>>8)&0xff;
  573.     buf[2] = (c>>16)&0xff;
  574.     buf[3] = c>>24;
  575. }
  576.  
  577. void MSG_WriteFloat (sizebuf_t *sb, float f)
  578. {
  579.     union
  580.     {
  581.         float   f;
  582.         int     l;
  583.     } dat;
  584.     
  585.     dat.f = f;
  586.     dat.l = LittleLong (dat.l);
  587.     
  588.     SZ_Write (sb, &dat.l, 4);
  589. }
  590.  
  591. void MSG_WriteString (sizebuf_t *sb, char *s)
  592. {
  593.     if (!s)
  594.         SZ_Write (sb, "", 1);
  595.     else
  596.         SZ_Write (sb, s, Q_strlen(s)+1);
  597. }
  598.  
  599. void MSG_WriteCoord (sizebuf_t *sb, float f)
  600. {
  601.     MSG_WriteShort (sb, (int)(f*8));
  602. }
  603.  
  604. void MSG_WriteAngle (sizebuf_t *sb, float f)
  605. {
  606.     MSG_WriteByte (sb, ((int)f*256/360) & 255);
  607. }
  608.  
  609. //
  610. // reading functions
  611. //
  612. int                     msg_readcount;
  613. qboolean        msg_badread;
  614.  
  615. void MSG_BeginReading (void)
  616. {
  617.     msg_readcount = 0;
  618.     msg_badread = false;
  619. }
  620.  
  621. // returns -1 and sets msg_badread if no more characters are available
  622. int MSG_ReadChar (void)
  623. {
  624.     int     c;
  625.  
  626.     if (msg_readcount+1 > net_message.cursize)
  627.     {
  628.         msg_badread = true;
  629.         return -1;
  630.     }
  631.         
  632.     c = (signed char)net_message.data[msg_readcount];
  633.     msg_readcount++;
  634.     
  635.     return c;
  636. }
  637.  
  638. int MSG_ReadByte (void)
  639. {
  640.     int     c;
  641.  
  642.     if (msg_readcount+1 > net_message.cursize)
  643.     {
  644.         msg_badread = true;
  645.         return -1;
  646.     }
  647.         
  648.     c = (unsigned char)net_message.data[msg_readcount];
  649.     msg_readcount++;
  650.     
  651.     return c;
  652. }
  653.  
  654. int MSG_ReadShort (void)
  655. {
  656.     int     c;
  657.  
  658.     if (msg_readcount+2 > net_message.cursize)
  659.     {
  660.         msg_badread = true;
  661.         return -1;
  662.     }
  663.         
  664.     c = (short)(net_message.data[msg_readcount]
  665.     + (net_message.data[msg_readcount+1]<<8));
  666.     
  667.     msg_readcount += 2;
  668.     
  669.     return c;
  670. }
  671.  
  672. int MSG_ReadLong (void)
  673. {
  674.     int     c;
  675.  
  676.     if (msg_readcount+4 > net_message.cursize)
  677.     {
  678.         msg_badread = true;
  679.         return -1;
  680.     }
  681.         
  682.     c = net_message.data[msg_readcount]
  683.     + (net_message.data[msg_readcount+1]<<8)
  684.     + (net_message.data[msg_readcount+2]<<16)
  685.     + (net_message.data[msg_readcount+3]<<24);
  686.     
  687.     msg_readcount += 4;
  688.     
  689.     return c;
  690. }
  691.  
  692. float MSG_ReadFloat (void)
  693. {
  694.     union
  695.     {
  696.         byte    b[4];
  697.         float   f;
  698.         int     l;
  699.     } dat;
  700.  
  701.     dat.b[0] =      net_message.data[msg_readcount];
  702.     dat.b[1] =      net_message.data[msg_readcount+1];
  703.     dat.b[2] =      net_message.data[msg_readcount+2];
  704.     dat.b[3] =      net_message.data[msg_readcount+3];
  705.     msg_readcount += 4;
  706.     
  707.     dat.l = LittleLong (dat.l);
  708.  
  709.     return dat.f;   
  710. }
  711.  
  712. char *MSG_ReadString (void)
  713. {
  714.     static char     string[2048];
  715.     int             l,c;
  716.  
  717.     l = 0;
  718.     do
  719.     {
  720.         c = MSG_ReadChar ();
  721.         if (c == -1 || c == 0)
  722.             break;
  723.         string[l] = c;
  724.         l++;
  725.     } while (l < sizeof(string)-1);
  726.     
  727.     string[l] = 0;
  728.     
  729.     return string;
  730. }
  731.  
  732. float MSG_ReadCoord (void)
  733. {
  734.     return MSG_ReadShort() * (1.0/8);
  735. }
  736.  
  737. float MSG_ReadAngle (void)
  738. {
  739.     return MSG_ReadChar() * (360.0/256);
  740. }
  741.  
  742.  
  743.  
  744. //===========================================================================
  745.  
  746. void SZ_Alloc (sizebuf_t *buf, int startsize)
  747. {
  748.     if (startsize < 256)
  749.         startsize = 256;
  750.     buf->data = Hunk_AllocName (startsize, "sizebuf");
  751.     buf->maxsize = startsize;
  752.     buf->cursize = 0;
  753. }
  754.  
  755.  
  756. void SZ_Free (sizebuf_t *buf)
  757. {
  758. //      Z_Free (buf->data);
  759. //      buf->data = NULL;
  760. //      buf->maxsize = 0;
  761.  
  762.     buf->cursize = 0;
  763. }
  764.  
  765. void SZ_Clear (sizebuf_t *buf)
  766. {
  767.     buf->cursize = 0;
  768. }
  769.  
  770. void *SZ_GetSpace (sizebuf_t *buf, int length)
  771. {
  772.     void    *data;
  773.  
  774.     if (buf->cursize + length > buf->maxsize)
  775.     {
  776.         if (!buf->allowoverflow)
  777.             Sys_Error ("SZ_GetSpace: overflow without allowoverflow set");
  778.         
  779.         if (length > buf->maxsize)
  780.             Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
  781.             
  782.         buf->overflowed = true;
  783.         Con_Printf ("SZ_GetSpace: overflow");
  784.         SZ_Clear (buf); 
  785.     }
  786.  
  787.     data = buf->data + buf->cursize;
  788.     buf->cursize += length;
  789.     
  790.     return data;
  791. }
  792.  
  793. void SZ_Write (sizebuf_t *buf, void *data, int length)
  794. {
  795.     Q_memcpy (SZ_GetSpace(buf,length),data,length);         
  796. }
  797.  
  798. void SZ_Print (sizebuf_t *buf, char *data)
  799. {
  800.     int             len;
  801.  
  802.     len = Q_strlen(data)+1;
  803.  
  804. // byte * cast to keep VC++ happy
  805.     if (buf->data[buf->cursize-1])
  806.         Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
  807.     else
  808.         Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
  809. }
  810.  
  811.  
  812. //============================================================================
  813.  
  814.  
  815. /*
  816. ============
  817. COM_SkipPath
  818. ============
  819. */
  820. char *COM_SkipPath (char *pathname)
  821. {
  822.     char    *last;
  823.  
  824.     last = pathname;
  825.     while (*pathname)
  826.     {
  827.         if (*pathname=='/')
  828.             last = pathname+1;
  829.         pathname++;
  830.     }
  831.     return last;
  832. }
  833.  
  834. /*
  835. ============
  836. COM_StripExtension
  837. ============
  838. */
  839. void COM_StripExtension (char *in, char *out)
  840. {
  841.     while (*in && *in != '.')
  842.         *out++ = *in++;
  843.     *out = 0;
  844. }
  845.  
  846. /*
  847. ============
  848. COM_FileExtension
  849. ============
  850. */
  851. char *COM_FileExtension (char *in)
  852. {
  853.     static char exten[8];
  854.     int             i;
  855.  
  856.     while (*in && *in != '.')
  857.         in++;
  858.     if (!*in)
  859.         return "";
  860.     in++;
  861.     for (i=0 ; i<7 && *in ; i++,in++)
  862.         exten[i] = *in;
  863.     exten[i] = 0;
  864.     return exten;
  865. }
  866.  
  867. /*
  868. ============
  869. COM_FileBase
  870. ============
  871. */
  872. void COM_FileBase (char *in, char *out)
  873. {
  874.     char *s, *s2;
  875.  
  876.     s = in + strlen(in) - 1;
  877.     
  878.     while (s != in && *s != '.')
  879.         s--;
  880.     
  881.     for (s2 = s ; *s2 && *s2 != '/' ; s2--)
  882.     ;
  883.     
  884.     if (s-s2 < 2)
  885.         strcpy (out,"?model?");
  886.     else
  887.     {
  888.         s--;
  889.         strncpy (out,s2+1, s-s2);
  890.         out[s-s2] = 0;
  891.     }
  892. }
  893.  
  894.  
  895. /*
  896. ==================
  897. COM_DefaultExtension
  898. ==================
  899. */
  900. void COM_DefaultExtension (char *path, char *extension)
  901. {
  902.     char    *src;
  903.  
  904. //
  905. // if path doesn't have a .EXT, append extension
  906. // (extension should include the .)
  907. //
  908.     src = path + strlen(path) - 1;
  909.  
  910.     while (*src != '/' && src != path)
  911.     {
  912.         if (*src == '.')
  913.             return;                 // it has an extension
  914.         src--;
  915.     }
  916.  
  917.     strcat (path, extension);
  918. }
  919.  
  920.  
  921. /*
  922. ==============
  923. COM_Parse
  924.  
  925. Parse a token out of a string
  926. ==============
  927. */
  928. char *COM_Parse (char *data)
  929. {
  930.     int             c;
  931.     int             len;
  932.  
  933.     len = 0;
  934.     com_token[0] = 0;
  935.     
  936.     if (!data)
  937.         return NULL;
  938.         
  939. // skip whitespace
  940. skipwhite:
  941.     while ( (c = *data) <= ' ')
  942.     {
  943.         if (c == 0)
  944.             return NULL;                    // end of file;
  945.         data++;
  946.     }
  947.     
  948. // skip // comments
  949.     if (c=='/' && data[1] == '/')
  950.     {
  951.         while (*data && *data != '\n')
  952.             data++;
  953.         goto skipwhite;
  954.     }
  955.     
  956.  
  957. // handle quoted strings specially
  958.     if (c == '\"')
  959.     {
  960.         data++;
  961.         while (1)
  962.         {
  963.             c = *data++;
  964.             if (c=='\"' || !c)
  965.             {
  966.                 com_token[len] = 0;
  967.                 return data;
  968.             }
  969.             com_token[len] = c;
  970.             len++;
  971.         }
  972.     }
  973.  
  974. // parse single characters
  975.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  976.     {
  977.         com_token[len] = c;
  978.         len++;
  979.         com_token[len] = 0;
  980.         return data+1;
  981.     }
  982.  
  983. // parse a regular word
  984.     do
  985.     {
  986.         com_token[len] = c;
  987.         data++;
  988.         len++;
  989.         c = *data;
  990.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  991.             break;
  992.     } while (c>32);
  993.     
  994.     com_token[len] = 0;
  995.     return data;
  996. }
  997.  
  998.  
  999. /*
  1000. ================
  1001. COM_CheckParm
  1002.  
  1003. Returns the position (1 to argc-1) in the program's argument list
  1004. where the given parameter apears, or 0 if not present
  1005. ================
  1006. */
  1007. int COM_CheckParm (char *parm)
  1008. {
  1009.     int             i;
  1010.  
  1011.     for (i=1 ; i<com_argc ; i++)
  1012.     {
  1013.         if (!com_argv[i])
  1014.             continue;               // NEXTSTEP sometimes clears appkit vars.
  1015.         if (!Q_strcmp (parm,com_argv[i]))
  1016.             return i;
  1017.     }
  1018.         
  1019.     return 0;
  1020. }
  1021.  
  1022. /*
  1023. ================
  1024. COM_CheckRegistered
  1025.  
  1026. Looks for the pop.txt file and verifies it.
  1027. Sets the "registered" cvar.
  1028. Immediately exits out if an alternate game was attempted to be started without
  1029. being registered.
  1030. ================
  1031. */
  1032. void COM_CheckRegistered (void)
  1033. {
  1034.     int             h;
  1035.     unsigned short  check[128];
  1036.     int                     i;
  1037.  
  1038.     COM_OpenFile("gfx/pop.lmp", &h);
  1039.     static_registered = 0;
  1040.  
  1041.     if (h == -1)
  1042.     {
  1043. #if WINDED
  1044.     Sys_Error ("This dedicated server requires a full registered copy of Quake");
  1045. #endif
  1046.         Con_Printf ("Playing shareware version.\n");
  1047.         if (com_modified)
  1048.             Sys_Error ("You must have the registered version to use modified games");
  1049.         return;
  1050.     }
  1051.  
  1052.     Sys_FileRead (h, check, sizeof(check));
  1053.     COM_CloseFile (h);
  1054.     
  1055.     for (i=0 ; i<128 ; i++)
  1056.         if (pop[i] != (unsigned short)BigShort (check[i]))
  1057.             Sys_Error ("Corrupted data file.");
  1058.     
  1059.     Cvar_Set ("cmdline", com_cmdline);
  1060.     Cvar_Set ("registered", "1");
  1061.     static_registered = 1;
  1062.     Con_Printf ("Playing registered version.\n");
  1063. }
  1064.  
  1065.  
  1066. void COM_Path_f (void);
  1067.  
  1068.  
  1069. /*
  1070. ================
  1071. COM_InitArgv
  1072. ================
  1073. */
  1074. void COM_InitArgv (int argc, char **argv)
  1075. {
  1076.     qboolean        safe;
  1077.     int             i, j, n;
  1078.  
  1079. // reconstitute the command line for the cmdline externally visible cvar
  1080.     n = 0;
  1081.  
  1082.     for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
  1083.     {
  1084.         i = 0;
  1085.  
  1086.         while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
  1087.         {
  1088.             com_cmdline[n++] = argv[j][i++];
  1089.         }
  1090.  
  1091.         if (n < (CMDLINE_LENGTH - 1))
  1092.             com_cmdline[n++] = ' ';
  1093.         else
  1094.             break;
  1095.     }
  1096.  
  1097.     com_cmdline[n] = 0;
  1098.  
  1099.     safe = false;
  1100.  
  1101.     for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
  1102.          com_argc++)
  1103.     {
  1104.         largv[com_argc] = argv[com_argc];
  1105.         if (!Q_strcmp ("-safe", argv[com_argc]))
  1106.             safe = true;
  1107.     }
  1108.  
  1109.     if (safe)
  1110.     {
  1111.     // force all the safe-mode switches. Note that we reserved extra space in
  1112.     // case we need to add these, so we don't need an overflow check
  1113.         for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
  1114.         {
  1115.             largv[com_argc] = safeargvs[i];
  1116.             com_argc++;
  1117.         }
  1118.     }
  1119.  
  1120.     largv[com_argc] = argvdummy;
  1121.     com_argv = largv;
  1122.  
  1123.     if (COM_CheckParm ("-rogue"))
  1124.     {
  1125.         rogue = true;
  1126.         standard_quake = false;
  1127.     }
  1128.  
  1129.     if (COM_CheckParm ("-hipnotic"))
  1130.     {
  1131.         hipnotic = true;
  1132.         standard_quake = false;
  1133.     }
  1134. }
  1135.  
  1136.  
  1137. /*
  1138. ================
  1139. COM_Init
  1140. ================
  1141. */
  1142. void COM_Init (char *basedir)
  1143. {
  1144.     byte    swaptest[2] = {1,0};
  1145.  
  1146. // set the byte swapping variables in a portable manner 
  1147.     if ( *(short *)swaptest == 1)
  1148.     {
  1149.         bigendien = false;
  1150.         BigShort = ShortSwap;
  1151.         LittleShort = ShortNoSwap;
  1152.         BigLong = LongSwap;
  1153.         LittleLong = LongNoSwap;
  1154.         BigFloat = FloatSwap;
  1155.         LittleFloat = FloatNoSwap;
  1156.     }
  1157.     else
  1158.     {
  1159.         bigendien = true;
  1160.         BigShort = ShortNoSwap;
  1161.         LittleShort = ShortSwap;
  1162.         BigLong = LongNoSwap;
  1163.         LittleLong = LongSwap;
  1164.         BigFloat = FloatNoSwap;
  1165.         LittleFloat = FloatSwap;
  1166.     }
  1167.  
  1168.     Cvar_RegisterVariable (®istered);
  1169.     Cvar_RegisterVariable (&cmdline);
  1170.     Cmd_AddCommand ("path", COM_Path_f);
  1171.  
  1172.     COM_InitFilesystem ();
  1173.     COM_CheckRegistered ();
  1174. }
  1175.  
  1176.  
  1177. /*
  1178. ============
  1179. va
  1180.  
  1181. does a varargs printf into a temp buffer, so I don't need to have
  1182. varargs versions of all text functions.
  1183. FIXME: make this buffer size safe someday
  1184. ============
  1185. */
  1186. char    *va(char *format, ...)
  1187. {
  1188.     va_list         argptr;
  1189.     static char             string[1024];
  1190.  
  1191.     va_start (argptr, format);
  1192.     vsprintf (string, format,argptr);
  1193.     va_end (argptr);
  1194.  
  1195.     return string;  
  1196. }
  1197.  
  1198.  
  1199. /// just for debugging
  1200. int     memsearch (byte *start, int count, int search)
  1201. {
  1202.     int             i;
  1203.     
  1204.     for (i=0 ; i<count ; i++)
  1205.         if (start[i] == search)
  1206.             return i;
  1207.     return -1;
  1208. }
  1209.  
  1210. /*
  1211. =============================================================================
  1212.  
  1213. QUAKE FILESYSTEM
  1214.  
  1215. =============================================================================
  1216. */
  1217.  
  1218. int     com_filesize;
  1219.  
  1220.  
  1221. //
  1222. // in memory
  1223. //
  1224.  
  1225. typedef struct
  1226. {
  1227.     char    name[MAX_QPATH];
  1228.     int             filepos, filelen;
  1229. } packfile_t;
  1230.  
  1231. typedef struct pack_s
  1232. {
  1233.     char    filename[MAX_OSPATH];
  1234.     int             handle;
  1235.     int             numfiles;
  1236.     packfile_t      *files;
  1237. } pack_t;
  1238.  
  1239. //
  1240. // on disk
  1241. //
  1242. typedef struct
  1243. {
  1244.     char    name[56];
  1245.     int             filepos, filelen;
  1246. } dpackfile_t;
  1247.  
  1248. typedef struct
  1249. {
  1250.     char    id[4];
  1251.     int             dirofs;
  1252.     int             dirlen;
  1253. } dpackheader_t;
  1254.  
  1255. #define MAX_FILES_IN_PACK       2048
  1256.  
  1257. char    com_cachedir[MAX_OSPATH];
  1258. char    com_gamedir[MAX_OSPATH];
  1259.  
  1260. typedef struct searchpath_s
  1261. {
  1262.     char    filename[MAX_OSPATH];
  1263.     pack_t  *pack;          // only one of filename / pack will be used
  1264.     struct searchpath_s *next;
  1265. } searchpath_t;
  1266.  
  1267. searchpath_t    *com_searchpaths;
  1268.  
  1269. /*
  1270. ============
  1271. COM_Path_f
  1272.  
  1273. ============
  1274. */
  1275. void COM_Path_f (void)
  1276. {
  1277.     searchpath_t    *s;
  1278.  
  1279.     Con_Printf ("Current search path:\n");
  1280.     for (s=com_searchpaths ; s ; s=s->next)
  1281.     {
  1282.         if (s->pack)
  1283.         {
  1284.             Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
  1285.         }
  1286.         else
  1287.             Con_Printf ("%s\n", s->filename);
  1288.     }
  1289. }
  1290.  
  1291. /*
  1292. ============
  1293. COM_WriteFile
  1294.  
  1295. The filename will be prefixed by the current game directory
  1296. ============
  1297. */
  1298. void COM_WriteFile (char *filename, void *data, int len)
  1299. {
  1300.     int             handle;
  1301.     char    name[MAX_OSPATH];
  1302.  
  1303. #ifdef AMIGA
  1304.     if (strlen(com_gamedir) == 0 ||
  1305.         com_gamedir[strlen(com_gamedir)-1] == ':')
  1306.         sprintf (name, "%s%s", com_gamedir, filename);
  1307.     else
  1308.         sprintf (name, "%s/%s", com_gamedir, filename);
  1309. #else
  1310.     sprintf (name, "%s/%s", com_gamedir, filename);
  1311. #endif
  1312.  
  1313.     handle = Sys_FileOpenWrite (name);
  1314.     if (handle == -1)
  1315.     {
  1316.         Sys_Printf ("COM_WriteFile: failed on %s\n", name);
  1317.         return;
  1318.     }
  1319.     
  1320.     Sys_Printf ("COM_WriteFile: %s\n", name);
  1321.     Sys_FileWrite (handle, data, len);
  1322.     Sys_FileClose (handle);
  1323. }
  1324.  
  1325.  
  1326. /*
  1327. ============
  1328. COM_CreatePath
  1329.  
  1330. Only used for CopyFile
  1331. ============
  1332. */
  1333. void    COM_CreatePath (char *path)
  1334. {
  1335.     char    *ofs;
  1336.  
  1337.     for (ofs = path+1 ; *ofs ; ofs++)
  1338.     {
  1339.         if (*ofs == '/')
  1340.         {       // create the directory
  1341.             *ofs = 0;
  1342.             Sys_mkdir (path);
  1343.             *ofs = '/';
  1344.         }
  1345.     }
  1346. }
  1347.  
  1348.  
  1349. /*
  1350. ===========
  1351. COM_CopyFile
  1352.  
  1353. Copies a file over from the net to the local cache, creating any directories
  1354. needed.  This is for the convenience of developers using ISDN from home.
  1355. ===========
  1356. */
  1357. void COM_CopyFile (char *netpath, char *cachepath)
  1358. {
  1359.     int             in, out;
  1360.     int             remaining, count;
  1361.     char    buf[4096];
  1362.  
  1363.     remaining = Sys_FileOpenRead (netpath, &in);            
  1364.     COM_CreatePath (cachepath);     // create directories up to the cache file
  1365.     out = Sys_FileOpenWrite (cachepath);
  1366.     
  1367.     while (remaining)
  1368.     {
  1369.         if (remaining < sizeof(buf))
  1370.             count = remaining;
  1371.         else
  1372.             count = sizeof(buf);
  1373.         Sys_FileRead (in, buf, count);
  1374.         Sys_FileWrite (out, buf, count);
  1375.         remaining -= count;
  1376.     }
  1377.  
  1378.     Sys_FileClose (in);
  1379.     Sys_FileClose (out);    
  1380. }
  1381.  
  1382. /*
  1383. ===========
  1384. COM_FindFile
  1385.  
  1386. Finds the file in the search path.
  1387. Sets com_filesize and one of handle or file
  1388. ===========
  1389. */
  1390. int COM_FindFile (char *filename, int *handle, FILE **file)
  1391. {
  1392.     searchpath_t    *search;
  1393.     char            netpath[MAX_OSPATH];
  1394.     char            cachepath[MAX_OSPATH];
  1395.     pack_t          *pak;
  1396.     int                     i;
  1397.     int                     findtime, cachetime;
  1398.  
  1399.     if (file && handle)
  1400.         Sys_Error ("COM_FindFile: both handle and file set");
  1401.     if (!file && !handle)
  1402.         Sys_Error ("COM_FindFile: neither handle or file set");
  1403.         
  1404. //
  1405. // search through the path, one element at a time
  1406. //
  1407.     search = com_searchpaths;
  1408.     if (proghack)
  1409.     {    // gross hack to use quake 1 progs with quake 2 maps
  1410.         if (!strcmp(filename, "progs.dat"))
  1411.             search = search->next;
  1412.     }
  1413.  
  1414.     for ( ; search ; search = search->next)
  1415.     {
  1416.     // is the element a pak file?
  1417.         if (search->pack)
  1418.         {
  1419.         // look through all the pak file elements
  1420.             pak = search->pack;
  1421.             for (i=0 ; i<pak->numfiles ; i++)
  1422.                 if (!strcmp (pak->files[i].name, filename))
  1423.                 {       // found it!
  1424. //                    Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
  1425.                     if (handle)
  1426.                     {
  1427.                         *handle = pak->handle;
  1428.                         Sys_FileSeek (pak->handle, pak->files[i].filepos);
  1429.                     }
  1430.                     else
  1431.                     {       // open a new file on the pakfile
  1432.                         *file = fopen (pak->filename, "rb");
  1433.                         if (*file)
  1434.                             fseek (*file, pak->files[i].filepos, SEEK_SET);
  1435.                     }
  1436.                     com_filesize = pak->files[i].filelen;
  1437.                     return com_filesize;
  1438.                 }
  1439.         }
  1440.         else
  1441.         {               
  1442.     // check a file in the directory tree
  1443.             if (!static_registered)
  1444.             {       // if not a registered version, don't ever go beyond base
  1445.                 if ( strchr (filename, '/') || strchr (filename,'\\'))
  1446.                     continue;
  1447.             }
  1448.             
  1449. #ifdef AMIGA
  1450.             if (strlen(search->filename) == 0 ||
  1451.                 search->filename[strlen(search->filename)-1] == ':')
  1452.                 sprintf (netpath, "%s%s",search->filename, filename);
  1453.             else
  1454.                 sprintf (netpath, "%s/%s",search->filename, filename);
  1455. #else
  1456.             sprintf (netpath, "%s/%s",search->filename, filename);
  1457. #endif
  1458.             
  1459.             findtime = Sys_FileTime (netpath);
  1460.             if (findtime == -1)
  1461.                 continue;
  1462.                 
  1463.         // see if the file needs to be updated in the cache
  1464.             if (!com_cachedir[0])
  1465.                 strcpy (cachepath, netpath);
  1466.             else
  1467.             {    
  1468. #if defined(_WIN32)
  1469.                 if ((strlen(netpath) < 2) || (netpath[1] != ':'))
  1470.                     sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1471.                 else
  1472.                     sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
  1473. #else
  1474.                 sprintf (cachepath,"%s%s", com_cachedir, netpath);
  1475. #endif
  1476.  
  1477.                 cachetime = Sys_FileTime (cachepath);
  1478.             
  1479.                 if (cachetime < findtime)
  1480.                     COM_CopyFile (netpath, cachepath);
  1481.                 strcpy (netpath, cachepath);
  1482.             }    
  1483.  
  1484.             Sys_Printf ("FindFile: %s\n",netpath);
  1485.             com_filesize = Sys_FileOpenRead (netpath, &i);
  1486.             if (handle)
  1487.                 *handle = i;
  1488.             else
  1489.             {
  1490.                 Sys_FileClose (i);
  1491.                 *file = fopen (netpath, "rb");
  1492.             }
  1493.             return com_filesize;
  1494.         }
  1495.         
  1496.     }
  1497.     
  1498.     Sys_Printf ("FindFile: can't find %s\n", filename);
  1499.     
  1500.     if (handle)
  1501.         *handle = -1;
  1502.     else
  1503.         *file = NULL;
  1504.     com_filesize = -1;
  1505.     return -1;
  1506. }
  1507.  
  1508.  
  1509. /*
  1510. ===========
  1511. COM_OpenFile
  1512.  
  1513. filename never has a leading slash, but may contain directory walks
  1514. returns a handle and a length
  1515. it may actually be inside a pak file
  1516. ===========
  1517. */
  1518. int COM_OpenFile (char *filename, int *handle)
  1519. {
  1520.     return COM_FindFile (filename, handle, NULL);
  1521. }
  1522.  
  1523. /*
  1524. ===========
  1525. COM_FOpenFile
  1526.  
  1527. If the requested file is inside a packfile, a new FILE * will be opened
  1528. into the file.
  1529. ===========
  1530. */
  1531. int COM_FOpenFile (char *filename, FILE **file)
  1532. {
  1533.     return COM_FindFile (filename, NULL, file);
  1534. }
  1535.  
  1536. /*
  1537. ============
  1538. COM_CloseFile
  1539.  
  1540. If it is a pak file handle, don't really close it
  1541. ============
  1542. */
  1543. void COM_CloseFile (int h)
  1544. {
  1545.     searchpath_t    *s;
  1546.  
  1547.     for (s = com_searchpaths ; s ; s=s->next)
  1548.         if (s->pack && s->pack->handle == h)
  1549.             return;
  1550.             
  1551.     Sys_FileClose (h);
  1552. }
  1553.  
  1554.  
  1555. /*
  1556. ============
  1557. COM_LoadFile
  1558.  
  1559. Filename are reletive to the quake directory.
  1560. Allways appends a 0 byte.
  1561. ============
  1562. */
  1563. cache_user_t *loadcache;
  1564. byte    *loadbuf;
  1565. int             loadsize;
  1566. byte *COM_LoadFile (char *path, int usehunk)
  1567. {
  1568.     int             h;
  1569.     byte    *buf;
  1570.     char    base[32];
  1571.     int             len;
  1572.  
  1573.     buf = NULL;     // quiet compiler warning
  1574.  
  1575. // look for it in the filesystem or pack files
  1576.     len = COM_OpenFile (path, &h);
  1577.     if (h == -1)
  1578.         return NULL;
  1579.     
  1580. // extract the filename base name for hunk tag
  1581.     COM_FileBase (path, base);
  1582.     
  1583.     if (usehunk == 1)
  1584.         buf = Hunk_AllocName (len+1, base);
  1585.     else if (usehunk == 2)
  1586.         buf = Hunk_TempAlloc (len+1);
  1587.     else if (usehunk == 0)
  1588.         buf = Z_Malloc (len+1);
  1589.     else if (usehunk == 3)
  1590.         buf = Cache_Alloc (loadcache, len+1, base);
  1591.     else if (usehunk == 4)
  1592.     {
  1593.         if (len+1 > loadsize)
  1594.             buf = Hunk_TempAlloc (len+1);
  1595.         else
  1596.             buf = loadbuf;
  1597.     }
  1598.     else
  1599.         Sys_Error ("COM_LoadFile: bad usehunk");
  1600.  
  1601.     if (!buf)
  1602.         Sys_Error ("COM_LoadFile: not enough space for %s", path);
  1603.         
  1604.     ((byte *)buf)[len] = 0;
  1605.  
  1606.     Draw_BeginDisc ();
  1607.     Sys_FileRead (h, buf, len);                     
  1608.     COM_CloseFile (h);
  1609.     Draw_EndDisc ();
  1610.  
  1611.     return buf;
  1612. }
  1613.  
  1614. byte *COM_LoadHunkFile (char *path)
  1615. {
  1616.     return COM_LoadFile (path, 1);
  1617. }
  1618.  
  1619. byte *COM_LoadTempFile (char *path)
  1620. {
  1621.     return COM_LoadFile (path, 2);
  1622. }
  1623.  
  1624. void COM_LoadCacheFile (char *path, struct cache_user_s *cu)
  1625. {
  1626.     loadcache = cu;
  1627.     COM_LoadFile (path, 3);
  1628. }
  1629.  
  1630. // uses temp hunk if larger than bufsize
  1631. byte *COM_LoadStackFile (char *path, void *buffer, int bufsize)
  1632. {
  1633.     byte    *buf;
  1634.  
  1635.     loadbuf = (byte *)buffer;
  1636.     loadsize = bufsize;
  1637.     buf = COM_LoadFile (path, 4);
  1638.     
  1639.     return buf;
  1640. }
  1641.  
  1642. /*
  1643. =================
  1644. COM_LoadPackFile
  1645.  
  1646. Takes an explicit (not game tree related) path to a pak file.
  1647.  
  1648. Loads the header and directory, adding the files at the beginning
  1649. of the list so they override previous pack files.
  1650. =================
  1651. */
  1652. pack_t *COM_LoadPackFile (char *packfile)
  1653. {
  1654.     dpackheader_t   header;
  1655.     int                             i;
  1656.     packfile_t              *newfiles;
  1657.     int                             numpackfiles;
  1658.     pack_t                  *pack;
  1659.     int                             packhandle;
  1660.     dpackfile_t             info[MAX_FILES_IN_PACK];
  1661.     unsigned short          crc;
  1662.  
  1663.     if (Sys_FileOpenRead (packfile, &packhandle) == -1)
  1664.     {
  1665. //              Con_Printf ("Couldn't open %s\n", packfile);
  1666.         return NULL;
  1667.     }
  1668.     Sys_FileRead (packhandle, (void *)&header, sizeof(header));
  1669.     if (header.id[0] != 'P' || header.id[1] != 'A'
  1670.     || header.id[2] != 'C' || header.id[3] != 'K')
  1671.         Sys_Error ("%s is not a packfile", packfile);
  1672.     header.dirofs = LittleLong (header.dirofs);
  1673.     header.dirlen = LittleLong (header.dirlen);
  1674.  
  1675.     numpackfiles = header.dirlen / sizeof(dpackfile_t);
  1676.  
  1677.     if (numpackfiles > MAX_FILES_IN_PACK)
  1678.         Sys_Error ("%s has %i files", packfile, numpackfiles);
  1679.  
  1680.     if (numpackfiles != PAK0_COUNT)
  1681.         com_modified = true;    // not the original file
  1682.  
  1683.     newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
  1684.  
  1685.     Sys_FileSeek (packhandle, header.dirofs);
  1686.     Sys_FileRead (packhandle, (void *)info, header.dirlen);
  1687.  
  1688. // crc the directory to check for modifications
  1689.     CRC_Init (&crc);
  1690.     for (i=0 ; i<header.dirlen ; i++)
  1691.         CRC_ProcessByte (&crc, ((byte *)info)[i]);
  1692.     if (crc != PAK0_CRC)
  1693.         com_modified = true;
  1694.  
  1695. // parse the directory
  1696.     for (i=0 ; i<numpackfiles ; i++)
  1697.     {
  1698.         strcpy (newfiles[i].name, info[i].name);
  1699.         newfiles[i].filepos = LittleLong(info[i].filepos);
  1700.         newfiles[i].filelen = LittleLong(info[i].filelen);
  1701.     }
  1702.  
  1703.     pack = Hunk_Alloc (sizeof (pack_t));
  1704.     strcpy (pack->filename, packfile);
  1705.     pack->handle = packhandle;
  1706.     pack->numfiles = numpackfiles;
  1707.     pack->files = newfiles;
  1708.     
  1709.     Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
  1710.     return pack;
  1711. }
  1712.  
  1713.  
  1714. /*
  1715. ================
  1716. COM_AddGameDirectory
  1717.  
  1718. Sets com_gamedir, adds the directory to the head of the path,
  1719. then loads and adds pak1.pak pak2.pak ... 
  1720. ================
  1721. */
  1722. void COM_AddGameDirectory (char *dir)
  1723. {
  1724.     int                             i;
  1725.     searchpath_t    *search;
  1726.     pack_t                  *pak;
  1727.     char                    pakfile[MAX_OSPATH];
  1728.  
  1729.     strcpy (com_gamedir, dir);
  1730.  
  1731. //
  1732. // add the directory to the search path
  1733. //
  1734.     search = Hunk_Alloc (sizeof(searchpath_t));
  1735.     strcpy (search->filename, dir);
  1736.     search->next = com_searchpaths;
  1737.     com_searchpaths = search;
  1738.  
  1739. //
  1740. // add any pak files in the format pak0.pak pak1.pak, ...
  1741. //
  1742.     for (i=0 ; ; i++)
  1743.     {
  1744. #ifdef AMIGA
  1745.         if (strlen(dir) == 0 ||
  1746.             dir[strlen(dir)-1] == ':')
  1747.             sprintf (pakfile, "%spak%i.pak", dir, i);
  1748.         else
  1749.             sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1750. #else
  1751.         sprintf (pakfile, "%s/pak%i.pak", dir, i);
  1752. #endif
  1753.         pak = COM_LoadPackFile (pakfile);
  1754.         if (!pak)
  1755.             break;
  1756.         search = Hunk_Alloc (sizeof(searchpath_t));
  1757.         search->pack = pak;
  1758.         search->next = com_searchpaths;
  1759.         com_searchpaths = search;               
  1760.     }
  1761.  
  1762. //
  1763. // add the contents of the parms.txt file to the end of the command line
  1764. //
  1765.  
  1766. }
  1767.  
  1768. /*
  1769. ================
  1770. COM_InitFilesystem
  1771. ================
  1772. */
  1773. void COM_InitFilesystem (void)
  1774. {
  1775.     int             i, j;
  1776.     char    basedir[MAX_OSPATH];
  1777.     searchpath_t    *search;
  1778.  
  1779. //
  1780. // -basedir <path>
  1781. // Overrides the system supplied base directory (under GAMENAME)
  1782. //
  1783.     i = COM_CheckParm ("-basedir");
  1784.     if (i && i < com_argc-1)
  1785.         strcpy (basedir, com_argv[i+1]);
  1786.     else
  1787.         strcpy (basedir, host_parms.basedir);
  1788.  
  1789.     j = strlen (basedir);
  1790.  
  1791.     if (j > 0)
  1792.     {
  1793.         if ((basedir[j-1] == '\\') || (basedir[j-1] == '/'))
  1794.             basedir[j-1] = 0;
  1795.     }
  1796.  
  1797. //
  1798. // -cachedir <path>
  1799. // Overrides the system supplied cache directory (NULL or /qcache)
  1800. // -cachedir - will disable caching.
  1801. //
  1802.     i = COM_CheckParm ("-cachedir");
  1803.     if (i && i < com_argc-1)
  1804.     {
  1805.         if (com_argv[i+1][0] == '-')
  1806.             com_cachedir[0] = 0;
  1807.         else
  1808.             strcpy (com_cachedir, com_argv[i+1]);
  1809.     }
  1810.     else if (host_parms.cachedir)
  1811.         strcpy (com_cachedir, host_parms.cachedir);
  1812.     else
  1813.         com_cachedir[0] = 0;
  1814.  
  1815. //
  1816. // start up with GAMENAME by default (id1)
  1817. //
  1818. #ifdef AMIGA
  1819.     if (strlen(basedir) == 0 ||
  1820.         basedir[strlen(basedir)-1] == ':') {
  1821.         COM_AddGameDirectory (va("%sid1", basedir) );
  1822.         if (COM_CheckParm ("-rogue"))
  1823.             COM_AddGameDirectory (va("%srogue", basedir) );
  1824.         if (COM_CheckParm ("-hipnotic"))
  1825.             COM_AddGameDirectory (va("%shipnotic", basedir) );
  1826.     } else {
  1827.         COM_AddGameDirectory (va("%s/id1", basedir) );
  1828.         if (COM_CheckParm ("-rogue"))
  1829.             COM_AddGameDirectory (va("%s/rogue", basedir) );
  1830.         if (COM_CheckParm ("-hipnotic"))
  1831.             COM_AddGameDirectory (va("%s/hipnotic", basedir) );
  1832.     }
  1833. #else
  1834.     COM_AddGameDirectory (va("%s/id1", basedir) );
  1835.  
  1836.     if (COM_CheckParm ("-rogue"))
  1837.         COM_AddGameDirectory (va("%s/rogue", basedir) );
  1838.     if (COM_CheckParm ("-hipnotic"))
  1839.         COM_AddGameDirectory (va("%s/hipnotic", basedir) );
  1840. #endif
  1841.  
  1842. //
  1843. // -game <gamedir>
  1844. // Adds basedir/gamedir as an override game
  1845. //
  1846.     i = COM_CheckParm ("-game");
  1847.     if (i && i < com_argc-1)
  1848.     {
  1849.         com_modified = true;
  1850. #ifdef AMIGA
  1851.         if (strlen(basedir) == 0 ||
  1852.             basedir[strlen(basedir)-1] == ':')
  1853.             COM_AddGameDirectory (va("%s%s", basedir, com_argv[i+1]));
  1854.         else
  1855.             COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
  1856. #else
  1857.         COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1]));
  1858. #endif
  1859.     }
  1860.  
  1861. //
  1862. // -path <dir or packfile> [<dir or packfile>] ...
  1863. // Fully specifies the exact serach path, overriding the generated one
  1864. //
  1865.     i = COM_CheckParm ("-path");
  1866.     if (i)
  1867.     {
  1868.         com_modified = true;
  1869.         com_searchpaths = NULL;
  1870.         while (++i < com_argc)
  1871.         {
  1872.             if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
  1873.                 break;
  1874.             
  1875.             search = Hunk_Alloc (sizeof(searchpath_t));
  1876.             if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
  1877.             {
  1878.                 search->pack = COM_LoadPackFile (com_argv[i]);
  1879.                 if (!search->pack)
  1880.                     Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
  1881.             }
  1882.             else
  1883.                 strcpy (search->filename, com_argv[i]);
  1884.             search->next = com_searchpaths;
  1885.             com_searchpaths = search;
  1886.         }
  1887.     }
  1888.  
  1889.     if (COM_CheckParm ("-proghack"))
  1890.         proghack = true;
  1891. }
  1892.